package com.mouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mouse.entity.*;
import com.mouse.entity.vo.GeneAlleleVO;
import com.mouse.mapper.*;
import com.mouse.service.GeneAlleleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mouse.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author godson
 * @since 2022-08-07
 */
@Service
public class GeneAlleleServiceImpl extends ServiceImpl<GeneAlleleMapper, GeneAllele> implements GeneAlleleService {
    @Autowired
    GeneCategoryMapper geneCategoryMapper;

    @Autowired
    private DiseaseMapper diseaseMapper;

    @Autowired
    private DoubleAllelesMapper doubleAllelesMapper;

    @Autowired
    private GeneCompositionMapper geneCompositionMapper;

    @Autowired
    private PhenotypeMapper phenotypeMapper;

    @Autowired
    private ReferenceMapper referenceMapper;

    @Autowired
    private GeneCompositionDoubleAllelesMapper geneCompositionDoubleAllelesMapper;

    @Autowired
    private MouseMapper mouseMapper;

    @Autowired
    private GeneCompositionReferenceMapper geneCompositionReferenceMapper;

    @Autowired
    private GenecategoryGenecompositionMapper genecategoryGenecompositionMapper;

    @Autowired
    private GeneHomologyMapper geneHomologyMapper;

    @Autowired
    private DoubleAllelesGeneAlleleMapper doubleAllelesGeneAlleleMapper;


    /***
     * 查询：根据一个字段信息查询表格信息，支持字段值类型为ID和String
     * @param page 分页设置
     * @param key 字段名
     * @param value 字段值
     * @return 查询结果
     */
    @Override
    public IPage<GeneAllele> getInfoByMap(IPage<GeneAllele> page, String key, String value) {
        long id;
        QueryWrapper<GeneAllele> wrapper = new QueryWrapper<>();
        if (StringUtil.isInteger(value)) {
            id = Long.parseLong(value);
            wrapper.eq(key, id);
        } else {
            wrapper.eq(key, value);
        }
        return baseMapper.selectPage(page, wrapper);
    }
    @Override
    public GeneAlleleVO getGeneAlleleVO(GeneAllele geneAllele) {


        /**
         *  key:ga.aid
         */

        Map<Long, List<DoubleAlleles>> doubleAllelesMap = new HashMap<>();

        /**
         * key:da.did
         */
        Map<Long, List<GeneComposition>> geneCompositionMap= new HashMap<>();

        /**
         * key:gcom.gcid
         */
        Map<Long, List<Phenotype>> phenotypeMap= new HashMap<>();

        /**
         * key:gcom.gcid
         */
        Map<Long, List<Reference>> referenceMap= new HashMap<>();

        /**
         * key:gcom.gcid
         */
        Map<Long, List<GeneCategory>> geneCategoryMap= new HashMap<>();

        /**
         * key:gc.gid
         */
        Map<Long, List<Disease>> diseaseMap= new HashMap<>();

        long id = geneAllele.getAid();
        GeneAlleleVO geneAlleleVO = new GeneAlleleVO();
        //DoubleAlleles相关
        List<DoubleAllelesGeneAllele> doubleAllelesGeneAlleleByAid = doubleAllelesGeneAlleleMapper.getDoubleAllelesGeneAlleleByAid(id);
        List<DoubleAlleles> doubleAllelesByAid = doubleAllelesMapper.getDoubleAllelesByAid(id);
        for(DoubleAllelesGeneAllele relation: doubleAllelesGeneAlleleByAid){
            doubleAllelesMap.computeIfAbsent(relation.getAid(), k -> new ArrayList<>());
            for (DoubleAlleles entity:doubleAllelesByAid){
                if(entity.getDid().equals(relation.getDid())) {
                    doubleAllelesMap.get(relation.getAid()).add(entity);
                }
            }
        }

        //GeneComposition相关
        List<GeneCompositionDoubleAlleles> gcdaByAid = geneCompositionDoubleAllelesMapper.getGCDAByAid(id);
        List<GeneComposition> geneCompositionByAid = geneCompositionMapper.getGeneCompositionByAid(id);
        for(GeneCompositionDoubleAlleles relation: gcdaByAid){
            geneCompositionMap.computeIfAbsent(relation.getDid(), k -> new ArrayList<>());
            for (GeneComposition entity:geneCompositionByAid){
                if(entity.getGcid().equals(relation.getDid())) {
                    geneCompositionMap.get(relation.getDid()).add(entity);
                }
            }
        }

        List<Mouse> mouseByAid = mouseMapper.getMouseByAid(id);
        List<Phenotype> phenotypeByAid = phenotypeMapper.getPhenotypeByAid(id);
        for(Mouse relation: mouseByAid){
            phenotypeMap.computeIfAbsent(relation.getGcid(), k -> new ArrayList<>());
            for (Phenotype entity:phenotypeByAid){
                if(entity.getPid().equals(relation.getPid())) {
                    phenotypeMap.get(relation.getGcid()).add(entity);
                }
            }
        }

        List<GeneCompositionReference> geneCompositionReferenceByAid = geneCompositionReferenceMapper.getGeneCompositionReferenceByAid(id);
        List<Reference> referenceByAid = referenceMapper.getReferenceByAid(id);
        for(GeneCompositionReference relation: geneCompositionReferenceByAid){
            referenceMap.computeIfAbsent(relation.getGcid(), k -> new ArrayList<>());
            for (Reference entity:referenceByAid){
                if(entity.getRid().equals(relation.getRid())) {
                    referenceMap.get(relation.getGcid()).add(entity);
                }
            }
        }

        //GeneCategory相关
        List<GenecategoryGenecomposition> genecategoryGenecompositionByAid = genecategoryGenecompositionMapper.getGenecategoryGenecompositionByAid(id);
        List<GeneCategory> geneCategoryByAid = geneCategoryMapper.getGeneCategoryByAid(id);
        for(GenecategoryGenecomposition relation: genecategoryGenecompositionByAid){
            geneCategoryMap.computeIfAbsent(relation.getGcid(), k -> new ArrayList<>());
            for (GeneCategory entity:geneCategoryByAid){
                if(entity.getGid().equals(relation.getGid())) {
                    geneCategoryMap.get(relation.getGcid()).add(entity);
                }
            }
        }

        List<GeneHomology> geneHomologyByAid = geneHomologyMapper.getGeneHomologyByAid(id);
        List<Disease> diseaseByAid = diseaseMapper.getDiseaseByAid(id);
        for(GeneHomology relation: geneHomologyByAid){
            diseaseMap.computeIfAbsent(relation.getGid(), k -> new ArrayList<>());
            for (Disease entity:diseaseByAid){
                if(entity.getSid().equals(relation.getSid())) {
                    diseaseMap.get(relation.getGid()).add(entity);
                }
            }
        }





        geneAlleleVO.setGeneAllele(geneAllele);

        geneAlleleVO.setDoubleAllelesMap(doubleAllelesMap);
        geneAlleleVO.setGeneCompositionMap(geneCompositionMap);
        geneAlleleVO.setPhenotypeMap(phenotypeMap);
        geneAlleleVO.setReferenceMap(referenceMap);
        geneAlleleVO.setGeneCategoryMap(geneCategoryMap);
        geneAlleleVO.setDiseaseMap(diseaseMap);

        return geneAlleleVO;
    }

//    @GetMapping("/gene-composition/{aid}/{currentPage}/{pageSize}")
//    public Result getGeneCompositionByAid(@PathVariable(name = "aid") Long aid,
//                                          @PathVariable Integer currentPage,
//                                          @PathVariable Integer pageSize) {
//        Page<GeneComposition> page = new Page<>(currentPage, pageSize);
//        Page<GeneComposition> pages = geneCompositionMapper.getGeneCompositionByAid(page, aid);
//        return Result.succ(pages);
//    }
//
//
//    @GetMapping("/reference/{aid}/{currentPage}/{pageSize}")
//    public Result getReferenceByAid(@PathVariable(name = "aid") Long aid,
//                                    @PathVariable Integer currentPage,
//                                    @PathVariable Integer pageSize) {
//        Page<Reference> page = new Page<>(currentPage, pageSize);
//        Page<Reference> pages = referenceMapper.getReferenceByAid(page, aid);
//        return Result.succ(pages);
//    }
//
//    @GetMapping("/phenotype/{aid}/{currentPage}/{pageSize}")
//    public Result getPhenotypeByAid(@PathVariable(name = "aid") Long aid,
//                                    @PathVariable Integer currentPage,
//                                    @PathVariable Integer pageSize) {
//        Page<Phenotype> page = new Page<>(currentPage, pageSize);
//        Page<Phenotype> pages = phenotypeMapper.getPhenotypeByAid(page, aid);
//        return Result.succ(pages);
//    }
//
//    @GetMapping("/gene-allele/{aid}/{currentPage}/{pageSize}")
//    public Result getDoubleAlleleByAid(@PathVariable(name = "aid") Long aid,
//                                       @PathVariable Integer currentPage,
//                                       @PathVariable Integer pageSize) {
//        Page<DoubleAlleles> page = new Page<>(currentPage, pageSize);
//        Page<DoubleAlleles> pages = doubleAllelesMapper.getDoubleAllelesByAid(page, aid);
//        return Result.succ(pages);
//    }
//
//    @GetMapping("/disease/{aid}/{currentPage}/{pageSize}")
//    public Result getDiseaseByAid(@PathVariable(name = "aid") Long aid,
//                                  @PathVariable Integer currentPage,
//                                  @PathVariable Integer pageSize) {
//        Page<Disease> page = new Page<>(currentPage, pageSize);
//        Page<Disease> pages = diseaseMapper.getDiseaseByAid(page, aid);
//        return Result.succ(pages);
//    }
//
//    @GetMapping("/geneCategory/{aid}/{currentPage}/{pageSize}")
//    public Result getGeneCategoryByAid(@PathVariable(name = "aid") Long aid,
//                                       @PathVariable Integer currentPage,
//                                       @PathVariable Integer pageSize) {
//        Page<GeneCategory> page = new Page<>(currentPage, pageSize);
//        Page<GeneCategory> pages = geneCategoryMapper.getGeneCategoryByAid(page, aid);
//        return Result.succ(pages);
//    }


}
